<template>
  <div v-if="visible" class="modal-overlay" @click.self="handleClose">
    <div class="modal-content">
      <h2>创建歌单</h2>
      
      <!-- 封面上传区域 -->
      <div class="cover-upload">
        <input 
          type="file" 
          ref="fileInput" 
          accept="image/jpeg,image/png,image/gif"
          class="file-input" 
          @change="handleFileChange"
        >
        <div 
          class="upload-area" 
          :class="{ 'has-image': previewUrl }"
          @click="triggerFileInput"
        >
          <img v-if="previewUrl" :src="previewUrl" alt="封面预览">
          <div v-else class="upload-placeholder">
            <div class="upload-icon">+</div>
            <span>点击上传封面</span>
          </div>
        </div>
      </div>

      <!-- 表单区域 -->
      <div class="form-group">
        <input 
          type="text" 
          v-model="formData.name" 
          placeholder="歌单名称 (必填)"
          maxlength="50"
        >
        <div class="input-counter">{{ formData.name.length }}/50</div>
      </div>

      <div class="form-group">
        <textarea 
          v-model="formData.description" 
          placeholder="歌单描述 (选填)"
          maxlength="200"
          rows="3"
        ></textarea>
        <div class="input-counter">{{ formData.description.length }}/200</div>
      </div>

      <!-- 按钮区域 -->
      <div class="modal-actions">
        <button class="cancel-btn" @click="handleClose">取消</button>
        <button 
          class="create-btn" 
          :disabled="!formData.name.trim()" 
          @click="handleCreate"
        >
          创建
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { createPlayList } from '@/api/playList';
import { ElMessage } from 'element-plus';

const props = defineProps<{
  visible: boolean
}>();

const emit = defineEmits<{
  (e: 'update:visible', value: boolean): void
  (e: 'created'): void
}>();

const fileInput = ref<HTMLInputElement | null>(null);
const previewUrl = ref('');

const formData = reactive({
  name: '',
  description: '',
  coverFile: null as File | null
});

// 处理文件选择
const handleFileChange = (event: Event) => {
  const input = event.target as HTMLInputElement;
  const file = input.files?.[0];
  
  if (file) {
    if (file.size > 2 * 1024 * 1024) {
      alert('图片大小不能超过2MB');
      return;
    }
    
    formData.coverFile = file;
    previewUrl.value = URL.createObjectURL(file);
  }
};

// 关闭模态框
const handleClose = () => {
  emit('update:visible', false);
  // 重置表单
  formData.name = '';
  formData.description = '';
  formData.coverFile = null;
  previewUrl.value = '';
};

// 创建歌单
const handleCreate = async () => {
  if (!formData.name.trim()) return;

  try {
    const result = await createPlayList({
      name: formData.name.trim(),
      description: formData.description.trim(),
      coverFile: formData.coverFile || undefined
    });

    if (result.success) {
      // 使用 type: 'success' 来确保显示绿色成功提示
      ElMessage({
        message: '创建成功',
        type: 'success',
        duration: 2000
      });
      emit('created');
      handleClose();
    } else {
      ElMessage({
        message: result.message || '创建失败',
        type: 'error',
        duration: 2000
      });
    }
  } catch (error) {
    console.error('创建歌单失败:', error);
    ElMessage({
      message: '创建歌单失败',
      type: 'error',
      duration: 2000
    });
  }
};

// 触发文件选择
const triggerFileInput = () => {
  fileInput.value?.click();
};

// 添加默认导出
defineOptions({
  name: 'CreatePlaylistModal'
});
</script>

<style scoped lang="scss">
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(8px);
}

.modal-content {
  background: var(--bgc-color-1);
  border-radius: 24px;
  padding: 32px;
  width: 90%;
  max-width: 480px;
  border: 1px solid rgba(var(--theme-color), 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(12px);
  position: relative;
  overflow: hidden;
}

.modal-content::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(145deg, rgba(var(--theme-color), 0.1), transparent);
  pointer-events: none;
}

h2 {
  color: var(--primary-color);
  margin-bottom: 24px;
  font-size: 1.5em;
  text-align: center;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  position: relative;
}

h2::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 2px;
  background: linear-gradient(to right, transparent, var(--theme-color), transparent);
  border-radius: 2px;
}

.cover-upload {
  margin-bottom: 24px;
}

.file-input {
  display: none;
}

.upload-area {
  width: 200px;
  height: 200px;
  margin: 0 auto;
  background: var(--bgc-color-2);
  border: 2px dashed var(--secondary-color);
  border-radius: 16px;
  cursor: pointer;
  overflow: hidden;
  transition: all 0.3s ease;
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.1);
}

.upload-area:hover {
  background: color-mix(in srgb, var(--bgc-color-2) 90%, var(--theme-color));
  border-color: var(--theme-color);
  transform: translateY(-2px);
  cursor: pointer;
}

.upload-placeholder {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: rgba(var(--theme-color), 0.7);
}

.upload-icon {
  font-size: 40px;
  margin-bottom: 8px;
}

.has-image {
  border-style: solid;
}

.has-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

input, textarea {
  width: 100%;
  padding: 12px 16px;
  background: var(--bgc-color-2);
  border: 1px solid rgba(var(--theme-color), 0.2);
  border-radius: 12px;
  color: var(--primary-color);
  font-size: 1em;
  transition: all 0.3s ease;
  box-shadow: 
    inset 0 2px 4px rgba(0, 0, 0, 0.1),
    0 0 0 transparent;
}

input:focus, textarea:focus {
  outline: none;
  border-color: var(--theme-color);
  background: color-mix(in srgb, var(--bgc-color-2) 90%, var(--theme-color));
  box-shadow: 
    inset 0 2px 4px rgba(0, 0, 0, 0.2),
    0 0 0 4px rgba(var(--theme-color), 0.1);
}

textarea {
  resize: vertical;
  min-height: 100px;
}

.input-counter {
  position: absolute;
  right: 8px;
  bottom: -20px;
  font-size: 0.8em;
  color: rgba(var(--primary-color), 0.6);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 16px;
  margin-top: 32px;
}

button {
  padding: 10px 24px;
  border-radius: 12px;
  font-size: 1em;
  cursor: pointer;
  transition: all 0.3s ease;
  letter-spacing: 0.5px;
  font-weight: 500;
}

.cancel-btn {
  background: transparent;
  border: 1px solid var(--secondary-color);
  color: var(--secondary-color);
  
  &:hover {
    background: rgba(var(--theme-color), 0.1);
    color: var(--primary-color);
    border-color: var(--theme-color);
  }
}

.create-btn {
  background: var(--theme-color);
  color: white;
  border: none;
  
  &:hover:not(:disabled) {
    background: var(--secondary-color);
  }
  
  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
}

@media (max-width: 480px) {
  .modal-content {
    padding: 24px;
  }

  .upload-area {
    width: 160px;
    height: 160px;
  }
}

/* 自定义文本框滚动条样式 */
textarea::-webkit-scrollbar {
  width: 8px;
}

textarea::-webkit-scrollbar-track {
  background: rgba(var(--bgc-color-2), 0.4);
  border-radius: 4px;
  margin: 8px 0;
}

textarea::-webkit-scrollbar-thumb {
  background: rgba(var(--theme-color), 0.4);
  border-radius: 4px;
  border: 2px solid rgba(var(--bgc-color-2), 0.4);
  transition: all 0.3s ease;
}

textarea::-webkit-scrollbar-thumb:hover {
  background: rgba(var(--theme-color), 0.6);
}

/* Firefox 的滚动条样式 */
textarea {
  scrollbar-width: thin;
  scrollbar-color: rgba(var(--theme-color), 0.4) rgba(var(--bgc-color-2), 0.4);
}
</style> 